Title: WooCommerce conditional tags - An overview

Publish Date: Fri, 11 Sep 2015 06:43:00 +0000

Categories: Uncategorized

Content:

Two days ago I talked about WordPress conditional tags.
Also WooCommerce has conditional tags.

I use them even more than WordPress conditional tags, but that's just because I mainly work on WooCommerce stuff like themes and plugins.
They are particularly useful in customizations, so if you want to add some custom features to your site, you should really learn how they work.



If you read some of my previous posts, you probably already noticed some of them in the snippets.
So let's learn what we have and how we can use them.

Shop page & WooCommerce

The two main conditional tags are is_shop() and is_woocommerce().
is_shop() returns true if the page shown in the main Shop page of your store, that one set in WooCommerce > Settings > Products > Display. It could be useful to show, in example, a specific banner for the shop page only, depending on how you decide to add the banner to your store.

An example:

https://gist.github.com/SiR-DanieL/8830f93997fef90b1138

is_woocommerce() instead, is more generic. It returns true whenever you are on a page that uses a WooCommerce template. Note though that the Cart and Checkout pages are not included, neither those pages where you are only using a WooCommerce shortcode.

The snippet above applies here as well, if you want to show the banner in any WooCommerce page.

Products

There are several conditional tags related to products.

The most obvious is is_product(). It returns true if the page shown is a single product page.
But you can also check if you are on a specific product tag or category archive, with is_product_tag() and is_product_category().
They both accept a parameter, an array of terms or a string, to check if you are on a tag or category archive page for that term.

An example:

https://gist.github.com/SiR-DanieL/09bc5e736b5d0ca1fabf

Other conditional tags

The remaining WooCommerce conditional tags are those needed to check if you are on the cart, checkout and account pages or on an endpoint.

To check if the page shown is the Cart page, you can use is_cart().
To check if the page shown is the Checkout page instead, use is_checkout(). This one is particularly useful for customizations for the payment gateways and shipping methods.
If you want to add/edit something only on the page My Account, use is_account_page(). Useful to print a special message for your customers in their account page.

If you want to do something on an endpoint instead, you can use is_wc_endpoint(). It accepts parameters to check if you are on a specific endpoint.
In example you could check if you are on the Thank you page by using is_wc_endpoint( 'order-received' ).

Learn more

If you want to learn more, you can check this page to know how to use the parameters accepted by these conditional tags.
